package org.lex.dbq.view.metadata;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.lex.dbq.model.jdbc.meta.VersionVo;
import org.lex.dbq.model.jdbc.meta.VoConverter;
import org.lex.swt.table.TableUtils;

public class VersionDialog extends Dialog {
	private Connection con = null;

	public VersionDialog(Shell shell, Connection con) {
		super(shell);
		this.con = con;
		this.setShellStyle(SWT.MAX | SWT.MIN | SWT.RESIZE);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Version");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		TableViewer table = TableUtils.createTable(parent, SWT.BORDER | SWT.FULL_SELECTION, VersionVo.COLUMN_NAMES);
		table.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
		try {
			DatabaseMetaData md = con.getMetaData();
			table.setInput(new VoConverter().getVersions(md));
			TableUtils.pack(table.getTable());
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return parent;
	}
}
